package com.sirap.fish.fetchers;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;

import com.google.common.collect.Lists;
import com.sirap.basic.component.Extractor;
import com.sirap.basic.component.Konstants;
import com.sirap.basic.thread.MasterItemsOriented;
import com.sirap.basic.thread.WorkerItemsOriented;
import com.sirap.basic.tool.D;
import com.sirap.basic.util.Amaps;
import com.sirap.basic.util.EmptyUtil;
import com.sirap.basic.util.IOUtil;
import com.sirap.basic.util.StrUtil;

public class YesImageFetchers {
	public static void main(String[] args) {
		Object va;
//		va = getAlbums(3);/
//		D.pl(countOfAlbum("https://www.fenun.cn/Item/Details/20912"));
//		va = getAlbumsByPage(1);
//		va = getAlbums();
//		va = getAlbum("https://www.yeitu.com/meinv/xinggan/20151030_10371.html");
//		va = imagesInPage("https://www.yeitu.com/meinv/xinggan/20151030_10371.html");
		va = searchAlbumsByPage("乳", 2);
//		D.pjsp(va);
	}
	
	public static List<Map<String, Object>> searchAlbums(String keyword) {
		List<Map<String, Object>> items = Lists.newArrayList();
		String nextpage = null;
		String current = "1";
		int max = 99, count = 0;
		do {
			if(count++ > max) {
				break;
			}
			SearchResultExtractor nick = new SearchResultExtractor(keyword, current);
			nick.process();
			List<Map<String, Object>> mexitems = nick.getItems();
			items.addAll(mexitems);
			//nextpage = nick.nextPage();
			D.pla(nextpage, current);
			if(StrUtil.equals(current, nextpage)) {
				nextpage = null;
			} else {
				current = nextpage;
			}
			D.pl(nextpage);
		} while(nextpage != null);
		
		return items;
	}
	
	public static Map<String, Object> searchAlbumsByPage(String keyword, Object page) {
		SearchResultExtractor nick = new SearchResultExtractor(keyword, page);
		nick.process();
		Map<String, Object> map = Amaps.newConcurrentHashMap();
		map.put("total", nick.getMaxPage());
		map.put("photos", nick.getItems());
		D.pjsp(map);
		
		return map;
	}
	
	public static List<Map<String, Object>> getAlbums() {
		Extractor<Map<String, Object>> frank = new Extractor<Map<String, Object>>() {
    		
			@Override
			public String getUrl() {
				String url = "https://www.yeitu.com/meinv/";
				useUTF8().showFetching();
				return url;
			}
//			@Override
			protected void fetchx() {
				String va = "ri2";
				String dir = StrUtil.occupy("E:/KDB/tasks/1126_AlbumSina/{0}.txt", va);
				source = IOUtil.readString(dir, Konstants.CODE_UTF8);
			}
			@Override
			protected void parse() {
				String r1 = "<li class=\"image-box\">(.+?)</li>";
				List<String> kids = StrUtil.findAllMatchedItems(r1, source);
				D.pl(kids.size());
				for(String kid : kids) {
					String regex = "<a href=\"([^\"]+)\" target=\"_blank\">\\s*";
					regex += "<img data-echo=\"([^\"]+)\".+?alt=\"([^\"]+)\"\\s*/>";
					Matcher ma = createMatcher(regex, kid);
					if(ma.find()) {
						Map<String, Object> row = Amaps.newLinkHashMap();
						row.put("url", ma.group(1));
						row.put("cover", ma.group(2));
						row.put("title", getPrettyText(ma.group(3)));

						String r3 = "<div class=\"tags\">(.+?)</div>";
						String taglist = StrUtil.findFirstMatchedItem(r3, kid);
						String r4 = ">([^<>]+)</a>";
						List<String> tags = StrUtil.findAllMatchedItems(r4, taglist, 1);
						row.put("tags", tags);
						mexItems.add(row);
					}
				}
				
			}
		};
		
		return frank.process().getItems();
	}
	
	public static Map<String, Object> getAlbum(String albumurl) {
		Map<String, Object> item = countAndTitleOf(albumurl);
		List<String> links = Lists.newArrayList(albumurl);
		int maxpage = Integer.parseInt(item.get("count") + "");
		for(int page = 2; page <= maxpage; page++) {
			links.add(albumurl.replace(".html", "_" + page + ".html"));
		}
		D.list(links);
		MasterItemsOriented<String, String> master = new MasterItemsOriented<>(links, new WorkerItemsOriented<String, String>() {

			@Override
			public List<String> process(String href) {
				return imagesInPage(href);
			}

		});
		List<String> list = master.getAllMexItems();
		
		item.put("list", list);
		
		return item;
	}
	
	public static List<String> imagesInPage(String url) {
		Extractor<String> frank = new Extractor<String>() {
    		
			@Override
			public String getUrl() {
				useGBK().showFetching();
				return url;
			}
			@Override
			protected void parse() {
				String r1 = "<div class=\"img_box\">(.+?)</div>";
				String kid = StrUtil.findFirstMatchedItem(r1, source);
				String regex = "src=\"([^\"]+)\"";
				mexItems = StrUtil.findAllMatchedItems(regex, kid, 1);
				item = StrUtil.findFirstMatchedItem(regex, source);
			}
		};
		
		return frank.process().getItems();
	}
	
	public static Map<String, Object> countAndTitleOf(String albumurl) {
		Extractor<Map<String, Object>> frank = new Extractor<Map<String, Object>>() {
    		
			@Override
			public String getUrl() {
				showFetching();
				return albumurl;
			}
			
			@Override
			protected void parse() {
				item = Amaps.newConcurrentHashMap();
				String regex = "<span class=\"imageset-sum\">/\\s*(\\d+)</span>";
				item.put("count", StrUtil.findFirstMatchedItem(regex, source));

				regex = "<h1>(.+?)</h1>";
				item.put("title", StrUtil.findFirstMatchedItem(regex, source));
			}
		};
		
		return frank.process().getItem();
	}
}

class SearchResultExtractor extends Extractor<Map<String, Object>> {

	private String keyword;
	private Object page;
	
	public SearchResultExtractor(String keyword, Object page) {
		this.keyword = keyword;
		this.page = page;
	}
	
	@Override
	public String getUrl() {
		String param = encodeURLParam(keyword);
		String url = StrUtil.occupy("https://www.yeitu.com/index.php?m=search&q={0}&page={1}", param, page);
		useUTF8().showFetching();
		return url;
	}
	
	public String getMaxPage() {
		String r1 = "page=\\d+\">(\\d+)<";
		List<String> kids = StrUtil.findAllMatchedItems(r1, source, 1);
		if(!EmptyUtil.isNullOrEmpty(kids)) {
			return kids.get(kids.size() - 1);
		} else {
			return "1";
		}
	}
	
	@Override
	protected void parse() {
		String r1 = "<li>\\s*<div class=\"list_box_img\">(.+?)</li>";
		List<String> kids = StrUtil.findAllMatchedItems(r1, source);
		for(String kid : kids) {
			Map<String, Object> row = Amaps.newLinkHashMap();
			String cover = StrUtil.findFirstMatchedItem("img src=\"([^\"]+)\"", kid);
			String href = StrUtil.findFirstMatchedItem("<h5><a href=\"([^\"]+)\"", kid);
			String title = StrUtil.findFirstMatchedItem("<h5>(.+?)</h5>", kid);
			String when = StrUtil.findFirstMatchedItem("<div class=\"date\">\\D+(.+?)</div>", kid);
			
			String r3 = "<span class=\"att\">(.+?)</span>";
			String taglist = StrUtil.findFirstMatchedItem(r3, kid);
			String r4 = ">([^<>]+)</a>";
			List<String> tags = StrUtil.findAllMatchedItems(r4, taglist, 1);
			row.put("tags", tags);

			row.put("url", href);
			row.put("cover", cover);
			row.put("title", getPrettyText(title));
			row.put("when", when.split(" ")[0]);
			D.pl(tags);
			mexItems.add(row);
		}
	}
}
